package net.zjitc.controller.client;

import net.zjitc.common.ServiceResp;
import net.zjitc.pojo.*;
import net.zjitc.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @Author admin
 * @Date 2019/4/18 8:34
 */
@Controller
@RequestMapping(value = "/Cart")
public class CartController {
    @Autowired
    CartService cartService;
    @Autowired
    CategoryService categoryService;
    @Autowired
    ProductService productService;
    @Autowired
    OrderService orderService;
    @Autowired
    OrderItemService orderItemService;
    public void top(Model model){
        List<Category> list = categoryService.findAll();
        model.addAttribute("list",list);
    }
    public void user_news(HttpServletRequest request, Model model){
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");
        List<Cart> cartList = cartService.findByuid(user.getId());
        for (Cart c:cartList
        ) {
            Products products = productService.findById(c.getPid());
            c.setProducts(products);
        }
        model.addAttribute("cart",cartList);
    }

    @RequestMapping(value = "/mycart")
    public String mycart(HttpServletRequest request, Model model){
        top(model);
        user_news(request,model);
        return "/client/cart.jsp";
    }

    @RequestMapping(value = "/myorder")
    public String myorder(HttpServletRequest request, Model model){
        top(model);
        user_news(request,model);
        return "/client/order.jsp";
    }

    @RequestMapping(value = "/addCart")
    @ResponseBody
    public ServiceResp addCart(Cart cart,HttpServletRequest request){
        //获取当前用户信息
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");
        cart.setUid(user.getId());
        int add = cartService.add(cart);
        if (add>0)
        {
            return ServiceResp.createBySuccess("添加成功",0);
        }
        return ServiceResp.createByERROR("添加失败",1);
    }

    @RequestMapping("/updateNumber")
    @ResponseBody
    public ServiceResp updateNumber(@RequestBody Cart cart,
                                    HttpServletRequest request){
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");
        cart.setUid(user.getId());
        int resultCount = cartService.updateNumber(cart);
        if (resultCount>0)
        {
            return ServiceResp.createBySuccess("商品修改成功",0);
        }
        return ServiceResp.createByERROR("商品修改失败",1);
    }


    @RequestMapping("/delete")
    @ResponseBody
    public ServiceResp delete(Integer id) {
        int resultCount = cartService.delete(id);
        if (resultCount > 0) {
            return ServiceResp.createBySuccess("商品移除成功",0);
        }
        return ServiceResp.createByERROR("商品移除失败",1);
    }


    @RequestMapping(value = "/createOrder")
    public String createOrder(Model model, HttpServletRequest request, Order order){
        top(model);
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");
        order.setUser_id(user.getId());
        order.setReceiverAddress(request.getParameter("receiverAddress"));
        order.setReceiverName(request.getParameter("receiverName"));
        order.setReceiverPhone(request.getParameter("receiverPhone"));
        order.setMoney(Double.valueOf(request.getParameter("money")));
        int result = orderService.add(order);
        if (result > 0)
        {
            OrderItem orderItem = new OrderItem();
            orderItem.setOid(order.getId());
            List<Cart> cartList = cartService.findByuid(order.getUser_id());
            for (Cart c:cartList
                 ) {
                orderItem.setPid(c.getPid());
                orderItem.setBuynum(c.getNumber());
                orderItemService.add(orderItem);
                cartService.delete(c.getId());
            }
            return "/newOrder.jsp";
        }
        return "/client/order.jsp";
    }
}
